// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var top = require("../top/top_main");
    var footer = require("../footer/footer_main");
    var commodityCategoryId;
     var storage = window.sessionStorage;
    var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
    
    var init = function(){
    	if(storage.getItem("mIndex2")!=null){
			pageIndex = storage.getItem("mIndex2");
		}
    	top.init();
    	footer.init();
    	router.init();
    	$("#head").removeClass("none");
    	$("#content").removeClass("none");
    	$("#footer").removeClass("none");
    	$("#content .w1366 div span").removeClass("active_topNav");
		$("#publicInquire").addClass("nav_active");
		$("#bulkstockGather").addClass("active_topNav");
    	inquireCommodityCategoryPageList();
    	inquireBulkstockGatherLatestList();
		initEvent(); 
    };
    
	var initEvent = function(){
		
		$("#commodityCategoryListBody").mouseover(function(){
			$("#commodityCategoryListBody").css({"min-height": "65px", "height": "auto"});
		});
		
		$("#commodityCategoryListBody").mouseleave(function(){
			$("#commodityCategoryListBody").css({"height": "65px"});
		});
		
		//自动补全
		$("#bulkstockCalculator").autocomplete(seajs.data.handler + "inquireBulkstockPageList.json", {
			minChars: 1,
			max: 10,
			width: 120,
			matchCase: false, //不区分大小写
			scroll: false,
			dataType: 'json',
			scrollHeight: 500,
			//此处为传递而外的参数
			extraParams: {
				pageSize: 50
			},
			//需要把data转换成json数据格式                      
			parse: function(data) {
				
				return $.map(eval(data.datas), function(row) {
					return {
						data: row,
						value: row.bulkstockName, //此处无需把全部列列出来，只是两个关键列
						result: row.bulkstockName + "(" + row.specificationName + ")"
					}
				});
			},
			formatItem: function(data, i, total) {
				if ($("#bulkstockCalculator").hasClass('nonemorelist')) {
					$("#bulkstockCalculator").removeClass('nonemorelist');
					return;
				};
				return "<table><tr><td>" + data.bulkstockName + "(" + data.specificationName + ")" + "</td></tr></table>";
			},
			formatMatch: function(data, i, total) {
				return data.bulkstockName;
			},
			formatResult: function(data, value) {
				return data.bulkstockName;
			}
		}).result(function(event, data, formatted) { //回调
			$('#bulkstockName').val(data.categoryName);
			if ($("#bulkstockName").hasClass('nonemorelist')) {
				$("#bulkstockName").removeClass('nonemorelist');
			} else {
				$("#bulkstockName").addClass('nonemorelist');
			};
			$("#bulkstockCalculator").attr("bulkstockInfoId", data.bulkstockInfoId);
		});
	
		//计算按钮
		$("#calculateButton").unbind("click").click(function(){
			var bulkstock = $(this).parents().find(".bulkstockName").val();
			var specification = bulkstock.substring(bulkstock.indexOf("(")+1, bulkstock.length-1);
			var quantity = $(this).parents().find(".bulkstockQuantity").val();
			var bulkstockName = bulkstock.substring(0, bulkstock.indexOf("("));
			inquireBulkstockGatherLatestList1(bulkstockName, specification, quantity);
		});
		
    };
    
     //查询商品价格
     var inquireBulkstockGatherLatestList1 = function(bulkstockName, specification, quantity){
     	$("#priceResult").html("0.00");
		base.fnAjax({
			action: "inquireBulkstockGatherLatestList.json",
			nocode: true,
			async: false,
			oData: {
                specification: specification,
                bulkstockName: bulkstockName
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var price = data.datas[0].price*quantity;
                	$("#priceResult").html(price.toFixed(2));
                } else {
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
    
    //大宗商品类别列表
    var inquireCommodityCategoryPageList = function(){
		base.fnAjax({
			action: "inquireBulkstockCategoryPageList.json",
			async: false,
			nocode: true,
			oData: {
				activeState: true,
				pageSize: 1000
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var tpl = $("#commodityCategoryListTpl").html();
                	var html = juicer(tpl, data);
                	$("#commodityCategoryListBody").html(html);
                	$(".type").unbind("click").click(function(){
                		$("#bulkstocGatherListBody").html("");
                		pageIndex=1;
                		inquireBulkstockGatherLatestList($(this).attr("commodityCategoryId"));
                		$(".type").removeClass("type_active");
                		$(this).addClass("type_active");
                	});
                } else {
                }
            }
		});
	};
	
	//大宗商品行情表
    var inquireBulkstockGatherLatestList = function(commodityCategoryId){
		base.fnAjax({
			action: "inquireBulkstockGatherLatestList.json",
			async: false,
			nocode: true,
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				bulkstockCategoryId: commodityCategoryId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var tpl = $("#bulkstocGatherListTpl").html();
                	var html = juicer(tpl, data);
                	$("#bulkstocGatherListBody").html(html);
                	totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
                	base.pageControl($("#changepage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							storage.setItem("mIndex2", pageIndex);
							inquireBulkstockGatherLatestList(commodityCategoryId);
						});
					riseAndFallColor();
//					添加监测跳转
					$(".publicMonitor").unbind("click").click(function(){
						var bulkstockname = $(this).parent().parent().attr("bulkstockname");
						var specification = $(this).parent().parent().attr("specification");
						var bulkstockCategoryId = $(this).parents("tr").attr("bulkstockCategoryId");
						location.href="../publicInquire/publicInquireMonitor.html?bulkstockName="+encodeURI(encodeURI(bulkstockname))+"&specification="+encodeURI(encodeURI(specification))+"&bulkstockCategoryId="+bulkstockCategoryId;
					});
                } else {
                }
            }
		});
	};
	
	//改变涨跌颜色
	var riseAndFallColor = function(){
		var riseAndFall = $(".riseAndFall");
		for(var i=0,j=riseAndFall.length;i<j;i++){
			if(riseAndFall[i].innerHTML.charAt(0)=="-"){
				$(riseAndFall[i]).css("color", "#33A55E");
			}else if(riseAndFall[i].innerHTML.charAt(0)=="+"){
				$(riseAndFall[i]).css("color", "#F1592A");
			}else{
			}
		}
	};

    return {
        init:init
    };
});